import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/Home.vue'),
    meta: { title: '书签导航' }
  },
  {
    path: '/admin',
    component: () => import('@/views/admin/Layout.vue'),
    redirect: '/admin/dashboard',
    meta: { requiresAdmin: true },
    children: [
      {
        path: 'dashboard',
        name: 'dashboard',
        component: () => import('@/views/admin/Dashboard.vue'),
        meta: { title: '控制面板', requiresAdmin: true }
      },
      {
        path: 'bookmarks',
        name: 'bookmarks',
        component: () => import('@/views/admin/Bookmarks.vue'),
        meta: { title: '书签管理', requiresAdmin: true }
      },
      {
        path: 'categories',
        name: 'categories',
        component: () => import('@/views/admin/Categories.vue'),
        meta: { title: '分类管理', requiresAdmin: true }
      },
      {
        path: 'import',
        name: 'import',
        component: () => import('@/views/admin/Import.vue'),
        meta: { title: '导入书签', requiresAdmin: true }
      }
    ]
  },
  {
    path: '/admin/login',
    name: 'login',
    component: () => import('@/views/admin/Login.vue'),
    meta: { title: '管理员登录' }
  },
  {
    path: '*',
    redirect: '/'
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 设置页面标题
router.afterEach((to) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
})

export default router
